Skip to content

feat(perps-controller): integrate Terminal API as primary market data source with HyperLiquid fallback#9137

Draft
michalconsensys wants to merge 11 commits into
mainfrom
feat/perps-terminal-api-markets
Draft

feat(perps-controller): integrate Terminal API as primary market data source with HyperLiquid fallback#9137
michalconsensys wants to merge 11 commits into
mainfrom
feat/perps-terminal-api-markets

Conversation

@michalconsensys

Copy link
Copy Markdown
Contributor

Description

Integrates the MetaMask Terminal API as the primary market data source for the Perps Controller, with a silent fallback to HyperLiquid. This is gated behind the perpsTerminalApiMarkets remote feature flag, checked per-fetch so toggling takes effect immediately.

What changed:

  • TerminalMarketService — new service that fetches structured market metadata from {terminalApiBaseUrl}/perpetuals with a 5-minute in-memory cache, response validation, and Sentry error logging.
  • MarketDataServicegetMarkets() now attempts the Terminal API first when the flag is enabled; on failure or empty response it falls back silently to the HyperLiquid provider. getMarketDataWithPrices() enriches provider data with Terminal API metadata (name, keywords, tags, categories).
  • PerpsController — wires the new service and reads perpsTerminalApiMarkets from RemoteFeatureFlagController on every call.
  • TypesPerpsPlatformDependencies gains terminalApiBaseUrl: string; PerpsMarketData gains optional keywords, tags, and categories fields.
  • transformMarketData() — accepts optional terminalMetadata to override static name/category maps per symbol.
  • Market searchgetMarketMatchRank and rankMarketsByQuery now index the keywords field for richer results.
  • ConstantsTERMINAL_API_CONFIG added with environment endpoint references and cache TTL.
  • Static fallback maps (HYPERLIQUID_ASSET_NAMES, HIP3_ASSET_MARKET_TYPES) remain intact for assets absent from the Terminal API.

Changelog

@metamask/perps-controller

  • Added: Terminal API integration for market data behind perpsTerminalApiMarkets feature flag, including TerminalMarketService, enriched metadata fields (keywords, tags, categories), and keyword-aware market search.

Related issues

N/A

Manual testing steps

Feature: Terminal API market data integration

Scenario: Markets loaded from Terminal API when flag is enabled

  • Given the perpsTerminalApiMarkets remote feature flag is true
  • And the Terminal API is reachable
  • When the user calls getMarkets()
  • Then markets are returned from the Terminal API
  • And the HyperLiquid provider is not called

Scenario: Silent fallback to HyperLiquid when Terminal API fails

  • Given the perpsTerminalApiMarkets remote feature flag is true
  • And the Terminal API returns an error or empty response
  • When the user calls getMarkets()
  • Then markets are returned from the HyperLiquid provider
  • And the error is logged to Sentry

Scenario: HyperLiquid used when flag is disabled

  • Given the perpsTerminalApiMarkets remote feature flag is false
  • When the user calls getMarkets()
  • Then the Terminal API is not called
  • And markets are returned from the HyperLiquid provider

Screenshots/Recordings

N/A — no UI changes.

Pre-merge author checklist

  • I've followed MetaMask Contributor Docs
  • I've completed the PR template to the best of my ability
  • I've included tests if applicable
  • I've documented my code using JSDoc format if applicable
  • I've applied the right labels on the PR
  • I've properly set the PR status:
    • In case of a draft, I've indicated my intention in the description

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and round trip)
  • I confirm that this PR addresses all acceptance criteria described in the ticket and includes the necessary testing evidence such as recordings and or parties involved
  • I've checked that the PR is targeting the correct branch

… source with HyperLiquid fallback

Add TerminalMarketService to fetch market data from the MetaMask Terminal API,
gated behind the `perpsTerminalApiMarkets` remote feature flag. When enabled,
the Terminal API is used as the primary source for market listings and metadata
(name, keywords, tags, categories), with a silent fallback to HyperLiquid on
failure. Also enhances market search to index against keyword fields.
…ets path

The Terminal API path in getMarkets was returning all markets without
respecting params.symbols or params.dex, unlike the provider fallback
path which applies those filters. This could cause callers requesting
specific symbols to receive the full market list.
…der data

The comment stated the two fetches ran in parallel, but the code awaited
the terminal metadata before starting the provider fetch. Use Promise.all
so both requests fly concurrently, reducing wall-clock latency when the
Terminal API flag is enabled.
…ormDependencies

Adding a required field to PerpsPlatformDependencies is a breaking change
for all existing consumers. Since the Terminal API feature is behind a
remote flag, the URL should be optional — when omitted the service is not
instantiated and the integration is disabled regardless of the flag.
…known values

The #extractMetadata method cast item.marketType to MarketType with an
unchecked `as` assertion. If the API returned an unrecognised value (e.g.
"derivatives"), it would propagate silently and could cause downstream
category filtering or UI issues. Now only values present in
MARKET_CATEGORIES are accepted; unknown strings are dropped.
… responses

Replace unsafe `as` type cast with per-item superstruct validation in
TerminalMarketService. Items that fail validation are filtered out and
logged instead of silently accepted. Also fix API path to /v1/perpetuals
and add a temporary test script for manual verification.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant